<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${baseInfo.basePackage}.${baseInfo.moduleName}.dao.${baseInfo.classForClassName}Dao">

    <resultMap type="${baseInfo.basePackage}.${baseInfo.moduleName}.model.${baseInfo.classForClassName}" id="${baseInfo.classForPrefix}Result">
    <#list baseInfo.columns as column>
        <result property="${column.attrNameForProperty}" column="${column.columnName}" jdbcType="${column.jdbcType}"/>
    </#list>
    </resultMap>
    <sql id="cols">
        <#list baseInfo.columns as column><#if !column_has_next>${column.columnName}<#else>${column.columnName},</#if></#list>
    </sql>
    <sql id="dynamicWhere">
        <where>
        <#list baseInfo.columns as column>
            <#if column.attrType == "String">
            <if test="${column.attrNameForProperty} != null<#if column.attrType == "String"> and ${column.attrNameForProperty} != ''</#if>">AND ${column.columnName} LIKE <#noparse>#{</#noparse>${column.attrNameForProperty},jdbcType=${column.jdbcType}}</if>
            <#elseif column.attrType == "Date">
            <if test="begin${column.attrNameForMethod} != null">AND ${column.columnName} &gt;= <#noparse>#{</#noparse>begin${column.attrNameForMethod},jdbcType=${column.jdbcType}}</if>
            <if test="end${column.attrNameForMethod} != null">AND ${column.columnName} &lt;= <#noparse>#{</#noparse>end${column.attrNameForMethod},jdbcType=${column.jdbcType}}</if>
            <#else>
            <if test="${column.attrNameForProperty} != null<#if column.attrType == "String"> and ${column.attrNameForProperty} != ''</#if>">AND ${column.columnName} = <#noparse>#{</#noparse>${column.attrNameForProperty},jdbcType=${column.jdbcType}}</if>
            </#if>
        </#list>
        </where>
    </sql>
    <insert id="add" parameterType="${baseInfo.basePackage}.${baseInfo.moduleName}.model.${baseInfo.classForClassName}">
        insert into ${baseInfo.tableName} (<include refid="cols"/>)
        VALUES(
        <#list baseInfo.columns as column>
            <#if !column_has_next>
            <#noparse>#{</#noparse>${column.attrNameForProperty},jdbcType=${column.jdbcType}}
        <#else>
            <#noparse>#{</#noparse>${column.attrNameForProperty},jdbcType=${column.jdbcType}},
        </#if>
        </#list>
        )
    </insert>

    <insert id="addList" parameterType="java.util.List" databaseId="oracle">
        insert into ${baseInfo.tableName} (<include refid="cols"/>)
        (
        <foreach collection="list" item="item" separator=" union all ">
            select
        <#list baseInfo.columns as column>
            <#if !column_has_next>
                <#noparse>#{</#noparse>item.${column.attrNameForProperty},jdbcType=${column.jdbcType}}
            <#else>
                <#noparse>#{</#noparse>item.${column.attrNameForProperty},jdbcType=${column.jdbcType}},
            </#if>
        </#list>
            from dual
        </foreach>
        )
    </insert>

    <insert id="addList" parameterType="java.util.List" databaseId="mysql">
        insert into ${baseInfo.tableName} (<include refid="cols"/>) values
        <foreach collection="list" item="item" separator="," index="index">
            (
        <#list baseInfo.columns as column>
            <#if !column_has_next>
                <#noparse>#{</#noparse>item.${column.attrNameForProperty}}
            <#else>
                <#noparse>#{</#noparse>item.${column.attrNameForProperty}},
            </#if>
        </#list>
            )
        </foreach>
    </insert>

    <insert id="addList" parameterType="java.util.List" databaseId="postgresql">
        insert into ${baseInfo.tableName} (<include refid="cols"/>) values
        <foreach collection="list" item="item" separator="," index="index">
            (
        <#list baseInfo.columns as column>
            <#if !column_has_next>
            <#noparse>#{</#noparse>item.${column.attrNameForProperty}}
            <#else>
            <#noparse>#{</#noparse>item.${column.attrNameForProperty}},
            </#if>
        </#list>
            )
        </foreach>
    </insert>

    <delete id="delById">
        delete from ${baseInfo.tableName} where ${baseInfo.primaryKey.columnName} = <#noparse>#{</#noparse>${baseInfo.primaryKey.attrNameForProperty},jdbcType=${baseInfo.primaryKey.jdbcType}}
    </delete>

    <delete id="delByIdArray">
        delete from ${baseInfo.tableName} where ${baseInfo.primaryKey.columnName} in
        <foreach  item="id" collection="array" index="index" open="(" separator="," close=")">
            <#noparse>#{</#noparse>id}
        </foreach>
    </delete>

    <update id="update" parameterType="${baseInfo.basePackage}.${baseInfo.moduleName}.model.${baseInfo.classForClassName}">
        update ${baseInfo.tableName}
        <set>
        <#list baseInfo.columns as column>
        <#if !column_has_next>
            <if test="${column.attrNameForProperty} != null">
                ${column.columnName} = <#noparse>#{</#noparse>${column.attrNameForProperty},jdbcType=${column.jdbcType}}
            </if>
            <#else>
            <if test="${column.attrNameForProperty} != null">
                ${column.columnName} = <#noparse>#{</#noparse>${column.attrNameForProperty},jdbcType=${column.jdbcType}},
            </if>
        </#if>
        </#list>
        </set>
        where ${baseInfo.primaryKey.columnName} = <#noparse>#{</#noparse>${baseInfo.primaryKey.attrNameForProperty},jdbcType=${baseInfo.primaryKey.jdbcType}}
    </update>

    <select id="getById" parameterType="${baseInfo.primaryKey.attrType}" resultMap="${baseInfo.classForPrefix}Result">
        select <include refid="cols"/> from ${baseInfo.tableName} where ${baseInfo.primaryKey.columnName} = <#noparse>#{</#noparse>${baseInfo.primaryKey.attrNameForProperty},jdbcType=${baseInfo.primaryKey.jdbcType}}
    </select>

    <select id="queryList" parameterType="java.util.Map" resultMap="${baseInfo.classForPrefix}Result">
        select <include refid="cols"/> from ${baseInfo.tableName}
        <include refid="dynamicWhere"/>
        <if test="orderField != null and orderField != ''">
            order by <#noparse>${orderField} ${orderSeq}</#noparse>
        </if>
    </select>

    <select id="queryAll" parameterType="java.util.Map" resultMap="${baseInfo.classForPrefix}Result">
        select <include refid="cols"/> from ${baseInfo.tableName}
        <choose>
            <when test="byId">
                <where>
                ${baseInfo.primaryKey.columnName} in
                    <foreach  item="id" collection="id" index="index" open="(" separator="," close=")">
                        <#noparse>#</#noparse>{id}
                    </foreach>
                    <if test="secret != null and secret != ''">AND secret &lt;= <#noparse>#</#noparse>{secret,jdbcType=VARCHAR}</if>
                    <if test="reportStatus != null and reportStatus != ''">AND report_status in (${reportStatus}) </if>
                </where>
            </when>
            <otherwise>
                <include refid="dynamicWhere"/>
            </otherwise>
        </choose>
    </select>
    <select id="reportList" resultMap="${baseInfo.classForPrefix}Result">
        select t.*
        from sys_report_master master
        left join sys_report_sub sub
        on master.id = sub.refId
        left join ${baseInfo.tableName} t
        on sub.businessId = t.id
        where master.tableName = <#noparse>#</#noparse>{tableName}
        and master.id = <#noparse>#</#noparse>{dataId}
        and sub.businessStatus = 1
    </select>

</mapper>